package com.company.cloud.article.modules.mgr.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.company.cloud.article.modules.cache.models.ArticleCategoryModel;
import com.company.cloud.article.modules.base.entity.ArticleCategoryInfo;
import com.company.cloud.article.modules.base.service.ArticleCategoryInfoService;
import com.company.cloud.article.modules.cache.service.ArticleCacheService;
import com.company.cloud.article.modules.mgr.controller.req.ArticleCategorySaveParam;
import com.company.cloud.article.modules.mgr.controller.req.ArticleCategoryUpdateParam;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseConstant.EnabledEnum;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.common.utils.CheckUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 文章分类管理
 */
@Slf4j
@Authc
@RestController
@RequestMapping("/mgr/cms/articleCategory")
@Api(tags = "Mgr_cms_文章分类管理")
public class ArticleCategoryMgrController {

    @Autowired
    private ArticleCategoryInfoService articleCategoryInfoService;
    @Autowired
    private ArticleCacheService articleCacheService;

    @GetMapping("/tree")
    @ApiOperation(value = "分类树", response = ArticleCategoryModel.class)
    public BaseResult tree(@RequestParam("companyNo") String companyNo) {
        return BaseResult.ok(articleCacheService.getArticleCategoryByCompanyDB(companyNo));
    }

    @PostMapping("/save")
    @ApiOperation(value = "新增", response = BaseResult.class)
    public BaseResult save(@RequestBody ArticleCategorySaveParam params) {
        ValidatorUtils.validate(params);
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        ArticleCategoryInfo entity = new ArticleCategoryInfo();
        entity.setCompanyNo(userInfo.getCompanyNo());
        if (CheckUtils.hasInt(params.getPid())) {
            entity.setPid(params.getPid());
        } else {
            entity.setPid(0);
        }
        entity.setTitle(params.getTitle());
        entity.setContent(params.getContent());
        entity.setSummary(params.getSummary());
        entity.setIcon(params.getIcon());
        entity.setType(params.getType());
        entity.setOrders(params.getOrders());
        entity.setMetaKeywords(params.getMetaKeywords());
        entity.setMetaDescription(params.getMetaDescription());
        entity.setCount(0);
        entity.addCreateParam(userInfo);
        entity.addEditParam(userInfo);

        entity.setTitleEn(params.getTitleEn());
        entity.setContentEn(params.getContentEn());
        entity.setSummaryEn(params.getSummaryEn());
        entity.setMetaKeywordsEn(params.getMetaKeywordsEn());
        entity.setMetaDescriptionEn(params.getMetaDescriptionEn());


        articleCategoryInfoService.save(entity);
        articleCacheService.removeCategoryByCache(entity.getCompanyNo());
        return BaseResult.ok();
    }

    @PostMapping("/edit")
    @ApiOperation(value = "修改", response = BaseResult.class)
    public BaseResult edit(@RequestBody ArticleCategoryUpdateParam params) {
        ValidatorUtils.validate(params);
        ArticleCategoryInfo entity = articleCategoryInfoService.getById(params.getId());
        if (entity == null || EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
            throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
        }
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        if (CheckUtils.hasInt(params.getPid())) {
            entity.setPid(params.getPid());
        } else {
            entity.setPid(0);
        }
        entity.setTitle(params.getTitle());
        entity.setContent(params.getContent());
        entity.setSummary(params.getSummary());
        entity.setIcon(params.getIcon());
        entity.setType(params.getType());
        entity.setOrders(params.getOrders());
        entity.setMetaKeywords(params.getMetaKeywords());
        entity.setMetaDescription(params.getMetaDescription());

        entity.setTitleEn(params.getTitleEn());
        entity.setContentEn(params.getContentEn());
        entity.setSummaryEn(params.getSummaryEn());
        entity.setMetaKeywordsEn(params.getMetaKeywordsEn());
        entity.setMetaDescriptionEn(params.getMetaDescriptionEn());

        entity.addEditParam(userInfo);
        articleCategoryInfoService.updateById(entity);
        articleCacheService.removeCategoryByCache(entity.getCompanyNo());
        return BaseResult.ok();
    }

    @GetMapping("/get")
    @ApiOperation(value = "获取", response = ArticleCategoryInfo.class)
    public BaseResult get(@RequestParam("id") Integer id) {
        ArticleCategoryInfo entity = articleCategoryInfoService.getById(id);
        if (entity == null || EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
            throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
        }
        return BaseResult.ok(entity);
    }

    @GetMapping("/delete")
    @ApiOperation(value = "删除")
    public BaseResult delete(@RequestParam("id") Integer id) {
        ArticleCategoryInfo entity = articleCategoryInfoService.getById(id);
        if (entity == null || EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
            throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
        }
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        entity.setRowsState(EnabledEnum.DISABLED.getCode());
        entity.addEditParam(userInfo);
        articleCategoryInfoService.updateById(entity);
        articleCacheService.removeCategoryByCache(entity.getCompanyNo());
        return BaseResult.ok();
    }
}

